package com.yanqu.road.server.cmd.palace;

import com.yanqu.road.entity.decoration.DecorationSimple;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.palace.CrossPalaceActivity;
import com.yanqu.road.server.manager.palace.CrossPalaceActivityMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_PALACE_BATTLE, desc = "跨服战斗")
public class CrossPalaceBattleCmd extends CrossCommand {
    private int activityId;
    private int pos;
    private long userId;
    private long enemyUserId;
    private long enemyServerId;
    private BigInteger myAbility;
    private List<DecorationSimple> decorationSimpleList = new ArrayList<>();
    private UserBaseInfo myUserInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceBattleCrossReqMsg reqMsg = EstateWarProto.PalaceBattleCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        pos = reqMsg.getPos();
        enemyUserId = reqMsg.getEnemyUserId();
        enemyServerId = reqMsg.getEnemyServerId();
        myAbility = new BigInteger(reqMsg.getMyAbility());
        if(reqMsg.getDecorationListCount() > 0){
            for(DecorationProto.DecorationSimpleTempMsg tempMsg : reqMsg.getDecorationListList()){
                DecorationSimple data = DecorationPb.parseDecorationSimple(tempMsg);
                if(null != data) {
                    decorationSimpleList.add(data);
                }
            }
        }
        myUserInfo = PlayerBasePb.parseBaseUserInfo(reqMsg.getMyUserInfo());
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossPalaceActivity crossPalaceActivity = CrossPalaceActivityMgr.getCrossPalaceActivity(activityId);
        if(null == crossPalaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        ret = crossPalaceActivity.battle(serverId, userId, myUserInfo, enemyServerId, enemyUserId, pos, myAbility, decorationSimpleList);
    }

    @Override
    public void sendData() {
        if(0 != ret) {
            EstateWarProto.PalaceWorshipRespMsg.Builder respMsg = EstateWarProto.PalaceWorshipRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_PALACE_WORSHIP, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }
}
